package gui.viewEntity;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.SplitPaneUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.metal.MetalSplitPaneUI;



import framework.classification.entities.ClAbstractEntity;
import framework.classification.library.AbstractClassfication;
import framework.classification.library.IOCLassificationDirectory;
import framework.entity.ACLEntity;

import gui.controller.Controller;
import gui.utils.AquaButtonPanel;
import gui.utils.DegradeVerticalPanel;
import gui.utils.DegradedPanel;
import gui.utils.JTabbedPaneWithCloseIcons;
import gui.utils.RoundedBorder;

public abstract class ViewACLEntity extends JPanel{

	
	
	protected ACLEntity aclEntity;
	protected AbstractClassfication classification;
	
	public JSplitPane split;
	public JTabbedPane tabbed;
	public DegradedPanel panel;
	
	public JList list1;
	public JList list2;
	
	public Controller controller;
	
	public JLabel metrique;
	
	public ViewACLEntity(ACLEntity aclEntity, AbstractClassfication classfication,Controller controller){
		this.aclEntity = aclEntity;
		metrique = new JLabel();
		this.classification = classfication;
		this.controller = controller;
		this.setLayout(new BorderLayout());
		this.add(createComponent(),BorderLayout.CENTER);
		update();
	
		
	}
	
	
	abstract public String getDescriptionLeft();
	abstract public String getDescriptionRight();
	abstract public ArrayList<String> getLeftParty();
	abstract public ArrayList<String> getRightParty();
	abstract public ArrayList<ClAbstractEntity> FillClassifyElement();
	
	
	private JComponent createComponent(){
		tabbed = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
		tabbed.setUI(new BasicTabbedPaneUI());
		tabbed.addChangeListener(new ChangeListener() {
			
			
			public void stateChanged(ChangeEvent arg0) {
				
				update();
			}
		});
		
		for (ClAbstractEntity cl : FillClassifyElement()){
			tabbed.add(cl.getName(),new ViewClEntity(aclEntity,cl,controller));
		}
		
		
		panel = new DegradedPanel(new Color(130,130,130),new Color(230,230,230));
		panel.setLayout(new BorderLayout());
		panel.add(createUpComponent());
		
		
		
		
		
		split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel,tabbed);
		split.setDividerSize(5);
		split.setDividerLocation(200);
		split.setBackground(new Color(230,230,230));
		setSplitPaneDividerColor(split,new Color(230,230,230));
	        

		return split;
	}
	
	private JComponent createRightComponent(){
		Box mainBox = Box.createVerticalBox();

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel(getDescriptionRight());
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setMinimumSize(new Dimension(20,25));
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));

		
		list2 = new JList(getRightParty().toArray());
		list2.addMouseListener(controller);
		JScrollPane slist2 = new JScrollPane(list2);
		slist2.setPreferredSize(new Dimension(350,400));
		slist2.setMaximumSize(new Dimension(350,400));

		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(slist2);
		mainBox.add(Box.createVerticalGlue());
		return mainBox;
		
		
		
		
		
	}
	private JComponent createLeftComponent(){
		Box mainBox = Box.createVerticalBox();

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel(getDescriptionLeft());
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setMinimumSize(new Dimension(20,25));
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));
		
		list1 = new JList(getLeftParty().toArray());
		list1.addMouseListener(controller);
		JScrollPane slist1 = new JScrollPane(list1);
		slist1.setPreferredSize(new Dimension(350,300));
		slist1.setMaximumSize(new Dimension(350,300));
		
		
		
		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(slist1);
		mainBox.add(Box.createVerticalGlue());
		return mainBox;

	}
	
	private JComponent createCenterComponent(){

		Box mainBox = Box.createVerticalBox();
		
		JPanel panelLabel = new DegradeVerticalPanel(new Color(0,0,0),new Color(0,0,0));
		JLabel label = new JLabel(aclEntity.getName(),JLabel.CENTER);
		label.setFont(new Font("Andalus", Font.BOLD, 17));
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,25));
		
		mainBox.add(panelLabel);
		
		return mainBox;
	}
	private JComponent createUpComponent(){
		
		Font font = new Font("Andalus", Font.BOLD,45);
		metrique = new JLabel("",JLabel.CENTER);
		metrique.setFont(new Font("Andalus", Font.BOLD, 12));
		metrique.setBorder(new RoundedBorder(Color.BLACK,25,25));
		metrique.setPreferredSize(new Dimension(200,100));
		metrique.setMaximumSize(new Dimension(200,100));
		metrique.setMinimumSize(new Dimension(200,100));
		metrique.setForeground(Color.BLACK);
		metrique.setOpaque(true);
		metrique.setBackground(Color.WHITE);
		Box hori = Box.createHorizontalBox();
		hori.add(Box.createHorizontalStrut(5));
		hori.add(createLeftComponent());
		hori.add(Box.createHorizontalStrut(5));
		hori.add(metrique);
		hori.add(Box.createHorizontalStrut(5));
		hori.add(createRightComponent());
		hori.add(Box.createHorizontalStrut(5));
		
		
		
		Box mainbox = Box.createVerticalBox();
		mainbox.add(Box.createVerticalStrut(15));
		mainbox.add(createCenterComponent());
		mainbox.add(Box.createVerticalStrut(15));
		mainbox.add(hori);
		
		return mainbox;
		
	}
	
	
	private void setSplitPaneDividerColor(JSplitPane splitPane, Color newDividerColor) {
	      SplitPaneUI splitUI = splitPane.getUI();
	      if (splitUI instanceof BasicSplitPaneUI) { // obviously this will not work if the ui doen't extend Basic...
	         int divSize = splitPane.getDividerSize();
	         BasicSplitPaneDivider div = ((BasicSplitPaneUI) splitUI).getDivider();
	         assert div != null;
	         Border divBorder = div.getBorder();
	         Border newBorder = null;
	         Border colorBorder = null;
	         
	         int insetsh = 0;
	         int insetsv = 0;
	         
	         if (divBorder != null) {
	            Insets i = divBorder.getBorderInsets(div);
	            insetsh = i.left + i.right;
	            insetsv = i.top + i.bottom;
	         }
	         
	         // this border uses a fillRect
	         colorBorder = BorderFactory.createMatteBorder(divSize - insetsv, divSize - insetsh, 0, 0, newDividerColor);
	 
	         if (divBorder == null) {
	            newBorder = colorBorder;
	         } else {
	            newBorder = BorderFactory.createCompoundBorder(divBorder, colorBorder);
	         }
	         div.setBorder(newBorder);
	      }
	   }
	
	public void update(){
		ViewClEntity vcl = (ViewClEntity) tabbed.getSelectedComponent();
		metrique.setText(vcl.getMetriqueLattice());
	}
	
	
}
